const {
  $util,
} = getApp();
const myaudio = wx.createInnerAudioContext();
Page({
  data: {
    list: [],
    hasMore: true,
    page: 1,
    isplay: false,
    current: -1, // 当前播放歌曲的index
    duration: '00:00',
    starttime: '00:00',
  },
  onLoad(opts) {
    this.setData({
      id: opts.id,
    }, () => {
      this.loadList();
    });
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '',
    });
  },
  onShow() {
    if (-1 !== this.data.current && this.data.isplay) {
      myaudio.play();
    }
  },
  loadList() {
    let {
      page,
      id,
      list,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/edu.partymusic.getMusicList',
      data: {
        id: id,
        page: page,
      },
      success: (res) => {
        let nList = res.list;
        if (page > 1) {
          nList = [...list, ...nList];
        }
        this.setData({
          ...res.data,
          list: nList,
          hasMore: res.list.length > 0,
          page: res.list.length > 0 ? page + 1 : page,
        });
        this.creatAudio();
      },
    });
  },
  onReachBottom() {
    this.data.hasMore && this.loadList();
  },
  audioPlay(e) {
    const {
      idx,
    } = e.currentTarget.dataset;
    let {
      list,
      current,
      isplay,
      duration,
      starttime,
    } = this.data;
    if (idx === current && isplay) return;
    if (idx !== current) {
      myaudio.seek(0);
      myaudio.src = list[idx].musicpath;
      duration = '00:00';
      starttime = '00:00';
    }
    myaudio.play();
    this.setData({
      isplay: true,
      current: idx,
      starttime: starttime,
      duration: duration,
    });
  },
  audioPaused(e) {
    myaudio.pause();
    this.setData({
      isplay: false,
    });
  },
  onUnload() {
    myaudio.stop();
  },
  creatAudio() {
    myaudio.onWaiting((res) => {
      console.log('加载中', res);
      wx.showLoading({
        title: '正在缓冲...',
      });
    });
    myaudio.onCanplay((res) => {
      console.log('可以播放', res);
      wx.hideLoading();
    });
    myaudio.onPlay((res) => {
      console.log('监听到音频开始播放', res);
      wx.hideLoading();
    });
    myaudio.onTimeUpdate((res) => {
      // 当前时间
      let s = parseInt(myaudio.currentTime);
      let minute = parseInt(s / 60);
      if (minute < 10) {
        minute = '0' + minute;
      }
      let second = s % 60;
      if (second < 10) {
        second = '0' + second;
      }
      // 总时长
      let max = parseInt(myaudio.duration);
      let durMinute = parseInt(max / 60);
      if (durMinute < 10) {
        durMinute = '0' + durMinute;
      }
      let durSecond = max % 60;
      if (durSecond < 10) {
        durSecond = '0' + durSecond;
      }
      this.setData({
        starttime: minute + ':' + second,
        duration: durMinute + ':' + durSecond,
      });
    });
    myaudio.onEnded(() => {
      console.log('播放结束');
      this.setData({
        isplay: false,
        current: -1,
      });
    });
    myaudio.onError((res) => {
      console.log('报错啦', res);
      let errMsg = '出错啦';
      this.setData({
        current: -1,
        isplay: false,
      });
      myaudio.stop();
      switch (res.errCode) {
        case 10001:
          errMsg = '系统错误';
          break;
        case 10002:
          errMsg = '网络错误';
          break;
        case 10003:
          errMsg = '文件错误';
          break;
        case 10004:
          errMsg = '格式错误';
          break;
        default:
          errMsg = '未知错误';
      }
      wx.showToast({
        title: errMsg,
        icon: 'none',
      });
    });
  },
  onShareAppMessage() {
    return {
      title: this.data.title,
    };
  },
});